---http帮助函数
local utilhttp = {
    baseuri = ""
}

-- ==========================
--default params
local d = Ding
local Logger = d.Logger
local resty_http = require("resty.http")
-- ==========================

local FAIL_RETRY_TIME = 1
local DEF_REQ_TIMEOUT = 3


local function http_get(uripath, params, cb)
    local n_time = 0
    local httpc = resty_http:new()

    local resp, err
    local execute 
    local fail_handle

    local suc_handle = function()
        Logger:debug("[HTTP_GET]Http <uri = %s> suc!", uripath .. utilhttp.baseuri)
        cb(true, resp)
    end

    fail_handle = function()  
        Logger:warning("[HTTP_GET]Http <uri = %s> failed!", uripath .. utilhttp.baseuri)
        if n_time < FAIL_RETRY_TIME then 
            execute()
        else 
            cb(false, err)
        end
    end
    execute = function()
        resp, err = httpc:request_uri(utilhttp.baseuri, {
            method = "GET",
            path = uripath, 
            headers = params["header"] or {}
        })
        if not resp then 
            fail_handle()
        else 
            suc_handle()
        end
    end
    execute()
end
utilhttp.http_get = http_get

return utilhttp